// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
// import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import App from './App'
// import md5 from 'md5'

import 'element-ui/lib/theme-chalk/index.css'
import ElementUI from 'element-ui'

//router & vuex
import router from './routers/index.js'
import store from './vuex/index'

//全局的ajax拦截
import resourceGlobalSet from './resource_set.js'

//component
import Mask from './components/widgets/Mask'
import Loading from './components/widgets/Loading.vue'
import Toast from './components/widgets/Toast.vue'

//CSS
import './assets/less/ionicons.min.css'
import './assets/less/main.less'
import './assets/less/base/button.less'

//common mixins
import common from './mixins/common.js'

//filters
import './filters/index'

import jQuery from 'jquery'
Vue.prototype.$jQuery = jQuery

Vue.use(VueResource)

Vue.use(ElementUI)


Vue.http.options.emulateJSON = true;
Vue.http.options.emulateHTTP = true;
Vue.http.options.withCredentials = true;

Vue.http.interceptors.push(resourceGlobalSet) // ajax 拦截

let _s = window.sessionStorage.getItem('_s');
if(!_s) {
  const sessionId = Math.random().toString(36).substr(2);
  window.sessionStorage.setItem('_s', sessionId);
  window.localStorage.setItem('_s', sessionId);
} else {
  window.localStorage.setItem('_s', _s);
}
/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  mixins: [common],
  components: {
    'app-mask': Mask,
    'app-loading': Loading,
    'app-toast': Toast
  },
  created () {
    let self = this;
    // 该事件是核心
    self.toSetSession();
    //sessionStorage 共享
    window.addEventListener('storage',function(event){
      if(event.key =='getSessionStorage') {
        // 已存在的标签页会收到这个事件
        localStorage.setItem('sessionStorage',JSON.stringify(sessionStorage));
        localStorage.removeItem('sessionStorage');

      } else if(event.key =='sessionStorage' && !(window.sessionStorage.getItem('_t'))) {
        // 新开启的标签页会收到这个事件
        let data =  JSON.parse(event.newValue);

        for(let key in data) {
          sessionStorage.setItem(key, data[key]);
        }
        if(event.oldValue == null) {
          self.getCurrentLang();
        }
      } else if (event.key == 'isLogout') {
        if(event.newValue == '1') {
          self.confirmLogout();
        } else if (event.newValue == '2') {
          if(window.location.href.indexOf('/login') > -1) {
            self.toSetSession();
            window.location.reload();
          }

          // 已存在的标签页会收到这个事件
          // self.$router.push({
          //   path: '/'
          // })
        }
      }
    });

    this.getCurrentLang();
    //绑定beforeunload事件
    jQuery(window).bind('beforeunload',function(){
      let tab = store.state.tab;
      if(tab && tab.list && tab.list.length > 0) {
        let ThisTab = tab.list[tab.active];
        window.sessionStorage.setItem('thisTab', JSON.stringify(ThisTab));
      }
    })
  },
  methods: {
    getCurrentLang () {
      // this.$store.dispatch('getCurLang');
    },
    confirmLogout () {
      let self = this;
      this.$store.dispatch('confirmLogout', {userToken: this.userToken}).then(() => {
        setTimeout(function() {
          this.$router.push({
            name: 'login'
          })
          window.localStorage.setItem('isAutoLogin', 0);
        }.bind(self),10);
      }, () => {
      })
    }
  },
  render: function (createElement) {
    return createElement(App)
  }
})
